//导入Ajax
import {
    ajax,
    isObject
} from "./ajax.js"

//获取本地的购物车数据
var cararr = true;
var bookcardata = {};
for(var i = 1;i<localStorage.length;i++){
    if(localStorage.getItem(localStorage.key(i))=="bookcar"){
        //获取书名
        var bookns = localStorage.key(i).split(".")
        var bookn = bookns[1]+"."+bookns[2]+"."+bookns[3]
        //获取要的数量
        var books = localStorage.getItem("cnum."+bookn)
        bookcardata[bookn] = books
        cararr = false
    }
}
// 渲染页面
if(cararr){
    $(".content .havegoods")[0].style.display = "none"
    $(".continue span")[0].style.display = "none"
    $(".content .empty")[0].style.display = "block"
}else{
    $(".content .empty")[0].style.display = "none"
// 调用ajax，获取购物车的内容****************************
ajax({
    url: "./json/book.json",
    dataType: "json",
    data: "",
    type: "get",
    cache: false,
    //调用成功执行函数
    success: function (data) {
        var str = "";
        //遍历bookcardata，购物车数据
        for(var key in bookcardata){
            var bookcc = key.split(".")
            str +=`
            <div class="goods ">
            <div>
                <input type="checkbox" class="checkone">
                <a href="./detail.html?bname=${escape(bookcc[0]+"."+bookcc[1]+"."+bookcc[2])}">
                    <img src=${data[bookcc[0]][bookcc[1]][bookcc[2]]["url"]} alt="">
                </a>
                <a href="./detail.html?bname=${escape(bookcc[0]+"."+bookcc[1]+"."+bookcc[2])}" class="bookname">
                    <span>
                    ${bookcc[2]}
                    </span>
                </a>
            </div>
            <div>${data[bookcc[0]][bookcc[1]][bookcc[2]]["文轩价"]}</div>
            <div>
            ${data[bookcc[0]][bookcc[1]][bookcc[2]]["文轩价"].split("￥")[1]*10*bookcardata[key]}
            </div>
            <div>
            ${data[bookcc[0]][bookcc[1]][bookcc[2]]["作者"]}
            </div>
             <div>
                 <button></button>
                 <input type="text"  value="${bookcardata[key]}">
                 <button></button>
             </div>
             <div>
                <b>删除</b>
             </div>
        </div>
            `
        }
        $(".carcont").html(str)


        
    },
    error: function (x) {
        alert(x)
    }
})
// 调用ajax，获取购物车的内容****************************

}
result()
//添加事件监听
$(".havegoods").on("click",function(e){
    //事件对象
    var e = e || window.event;
    //事件目标
    var target = e.target || e.srcElement;
    //当点击全选********
    if(target.className=="checkall1"){
        //判断是否全选
        if(target.checked){
            for(var i =0;i<$(".goods input[type='checkbox']").length;i++){
                $(".goods input[type='checkbox']")[i].checked = true
            }
        }else{
            for(var i =0;i<$(".goods input[type='checkbox']").length;i++){
                $(".goods input[type='checkbox']")[i].checked = false
            }
        }
        result()
    }
    //当点击全选********

    //增加或减少数量************
    if(target.nodeName=="BUTTON"){
        //判断是加还是减
        if(target===target.parentNode.children[0]){
            //当为减时
            if(target.parentNode.children[1].value<=1){
                target.parentNode.children[1].value = 1
                return
            }
            target.parentNode.children[1].value--
        }else{
            target.parentNode.children[1].value++
        }
        var price = target.parentNode.parentNode.children[1].innerHTML.split("￥")[1]
        target.parentNode.parentNode.children[2].innerHTML = parseInt(price*target.parentNode.children[1].value*10)
        result()
    }
    //增加或减少数量************

    //单项商品是否选择
    if(target.className=="checkone"){
        checkone()
    }
    function checkone(){
        var check =true;
        for(var i =0;i<$(".goods input[type='checkbox']").length;i++){
            if($(".goods input[type='checkbox']")[i].checked == true){

            }else{

                check =false;
            }
        }
        $(".checkall1")[0].checked = check
        result()
    }
    //单项商品是否选择

    //删除单项商品***
    if(target.innerHTML=="删除"){
        var na = unescape(target.parentNode.parentNode.children[0].children[1].getAttribute("href")).split("?")[1].split("=")[1]
        window.localStorage.removeItem('c.'+na)
        window.localStorage.removeItem('cnum.'+na)
        target.parentNode.parentNode.remove()
        checkone()
    }
    //删除单项商品***


    //删除所选商品***
    if(target.className=="sum-left"){
        for(var i =0;i<$(".goods input[type='checkbox']").length;i++){
            if($(".goods input[type='checkbox']")[i].checked == true){
                var na = unescape($(".goods input[type='checkbox']")[i].parentNode.parentNode.children[0].children[1].getAttribute("href")).split("?")[1].split("=")[1]
                window.localStorage.removeItem('c.'+na)
                window.localStorage.removeItem('cnum.'+na)
                $(".goods input[type='checkbox']")[i].parentNode.parentNode.remove()
                i--
            }else{

            }
        }
        result()
    }
    //删除所选商品***

})
    //计算总价*******************
    function result(){
        //总价
        var total = 0
        var num1 = 0
        for(var j=0;j<$(".goods").length;j++){
            if($(".goods")[j].children[0].children[0].checked){
                //当前商品的单价
                var price1= Number($(".goods")[j].children[1].innerHTML.split("￥")[1])
                //当前商品的数量
                var num = $(".goods")[j].children[4].children[1].value
                total += (price1*num)
                num1++
            }
        }
        total = total.toFixed(2)
        $(".sum-right p")[0].children[0].innerHTML = "￥"+total
        $(".sum-right")[0].children[0].children[0].innerHTML = num1
        $(".total p span").html("￥"+total)
    }
    //计算总价*******************